package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.FamilyBuyBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.HomeRetailPresenter;
import cn.xfdzx.android.apps.shop.presenter.StoreInfoPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.RoundedFormation;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.ShoppingCartAnimationView;
import cn.xfdzx.android.apps.shop.view.TagTextView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：零售首页 家庭购 商品
 */
public class RetailFamilyGoodAdapter extends RecyclerView.Adapter<RetailFamilyGoodAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<FamilyBuyBeen.DataBean.FamilyShoppingActivityBean> mList;
    private Activity mContext;
    private int proportion;
    private HomeRetailPresenter mPresenter;
    RoundedCorners roundedCorners;
    RequestOptions options;

    public RetailFamilyGoodAdapter(Activity context, List<FamilyBuyBeen.DataBean.FamilyShoppingActivityBean> list, HomeRetailPresenter presenter) {
        this.mList = list;
        this.mContext = context;
        this.mPresenter = presenter;
        roundedCorners = new RoundedCorners(20);
        options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_retail_family_goods, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (proportion != 0) {
            proportion = holder.ivFineSelect.getWidth() / holder.ivFineSelect.getHeight();
        }

        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivFineSelect);
        holder.tvCarNum.setText(mList.get(position).getCart_num() + "");
        if (mList.get(position).getCart_num() == 0) {
            holder.tvCarNum.setVisibility(View.GONE);
        } else {
            holder.tvCarNum.setVisibility(View.VISIBLE);
        }

        if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
            holder.tvPriceBox.setText("¥" + mList.get(position).getMarketprice());
            holder.tvSaleNum.setVisibility(View.GONE);
        } else {
            holder.tvPriceBox.setText("¥" + mList.get(position).getDiscountprice());
            holder.tvSaleNum.setText("¥" + mList.get(position).getMarketprice());
            holder.tvSaleNum.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            holder.tvSaleNum.setVisibility(View.VISIBLE);
        }

        holder.ttvContent.setText(mList.get(position).getTitle());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });

        holder.ivCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (EmptyUtil.isEmpty(APP_TOKEN)){
                    Intent intent = new Intent(mContext, OneLoginMainActivity.class);
                    mContext.startActivity(intent);
                    return;
                }

                mPresenter.addCar("0", "", mList.get(position).getGoods_id() + "", "1", position,mList.get(position).getCart_num()+1);

            }
        });
    }





    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_fine_select)
        ImageView ivFineSelect;
        @BindView(R.id.tv_content)
        TagTextView ttvContent;
        @BindView(R.id.tv_price_box)
        TextView tvPriceBox;
        @BindView(R.id.tv_sale_num)
        TextView tvSaleNum;
        @BindView(R.id.tv_car_num)
        TextView tvCarNum;
        @BindView(R.id.iv_car)
        ImageView ivCar;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
